package com.jswlw.web.fitler;

import com.alibaba.fastjson.JSON;
import com.jswlw.web.manager.SessionAdminManager;
import com.jswlw.entity.base.User;
import com.jswlw.web.manager.DistributorUser;
import com.jswlw.web.manager.SessionApiManager;
import com.jswlw.web.manager.SessionDistributorManager;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Objects;

import static com.jswlw.common.Constants.USER_SESSION;

/**
 * 权限拦截器
 */
@Slf4j
@Component
@WebFilter
public class AuthFilter implements Filter {

    public static Logger logger = LoggerFactory.getLogger(AuthFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = ((HttpServletResponse) servletResponse);
        String uri = httpServletRequest.getRequestURI();
        if (uri.contains("devApi")) {
            log.info("uri:"+uri);
            log.info("api接口参数:" + JSON.toJSONString(httpServletRequest.getParameterMap()));
        }
        if (uri.contains("app")
                || uri.contains("static")
                || uri.contains("MP_verify_tthSAZOpc0keX2Lx")
                || uri.contains("swagger")
                || uri.contains("api-docs")
                || uri.contains("csrf")
                || uri.contains("devApi")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        //经销商平台
        if (uri.startsWith("/distributor")) {
            if (uri.toLowerCase().contains("login")) {
                filterChain.doFilter(servletRequest, servletResponse);
                return;
            }
            Object obj = httpServletRequest.getSession().getAttribute("DistributorUser");
            if (Objects.isNull(obj)) {
                //需要登录
                String login = "<script>window.parent.location.href='" + httpServletRequest.getContextPath()
                        + "/distributor/login/index';</script>";
                httpServletResponse.getWriter().write(login);
                return;
            }
            SessionDistributorManager.setCurrentUser((DistributorUser) obj);

            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        if (uri.startsWith("/api")) {
            SessionApiManager.setCurrentUser(((HttpServletRequest) servletRequest).getHeader("openId"));
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        if (!uri.contains("login")) {
            //不是登录或者退出请求，则需要判断用户是否登录
            HttpSession session = httpServletRequest.getSession();
            if (session.getAttribute(USER_SESSION) == null) {
                //跳转到登录页面
                String login = "<script>window.parent.location.href='" + httpServletRequest.getContextPath()
                        + "/login';</script>";
                httpServletResponse.getWriter().write(login);
                return;
            } else {
                SessionAdminManager.setCurrentUser((User) session.getAttribute(USER_SESSION));
            }
        }
        filterChain.doFilter(servletRequest, servletResponse);

    }

    @Override
    public void destroy() {

    }
}
